/**
 * This file contains the d51.MySQLConsole code
 */
var d51 = d51 || {};
d51.MySQLConsole = {};
d51.MySQLConsole.execute = function() {
	try {
	var cmd = 'mysql -t -e "' + document.getElementById('query').value + '"';
	if (document.getElementById('hostname').value != '') {
		cmd += ' -h ' + document.getElementById('hostname').value;
	}
	if (document.getElementById('username').value != '') {
		cmd += ' -u ' + document.getElementById('username').value;
	}
	if (document.getElementById('password').value != '') {
		cmd += ' -p' + document.getElementById('password').value;
	}
	
	
	var result = new Object();
	var error = new Object();
	var runService = Components.classes['@activestate.com/koRunService;1'].getService(Components.interfaces.koIRunService);
	var runResult = runService.RunAndCaptureOutput(cmd,
												   "",
												   "",
												   "",
												   result,
												   error);
	
	if (error.value != "") {
		alert("There was an error:\n\n" + error.value);
		return;
	}
	
	d51.MySQLConsole.display(result.value);
	} catch(e) {
		alert(e);
	}
}

d51.MySQLConsole.display = function(input) {
//function d51_displayResult(input) {
	document.getElementById('query_results').value = input;
}

//d51.MySQLConsole.re = new RegExp("\n\n\n$");
d51.MySQLConsole.poll = function() {
//function d51_poll() {
	var re = new RegExp("\n\n\n$");
	if (re.test(document.getElementById('query').value)) {
		d51.MySQLConsole.execute();
	}
}
